package small.hero.pageview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;

public class AnimableScrollablePageContainer extends ScrollablePageContainer {

    public interface AnimationListener {
        public boolean onPrepare(View animToView, int animToIndex);
    }

    private boolean mIsAnimating = false;
    private boolean mInAnimRuning = false;
    private boolean mOutAnimRuning = false;
    private AnimationListener mAnimationListener;
    /**
     * Initiate a new instance of {@link ScrollablePageContainer}.
     *
     * @param context
     */
    public AnimableScrollablePageContainer(Context context) {
        super(context);
    }

    /**
     * Initiate a new instance of {@link ScrollablePageContainer}.
     *
     * @param context
     * @param attrs
     */
    public AnimableScrollablePageContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Initiate a new instance of {@link ScrollablePageContainer}.
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public AnimableScrollablePageContainer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {
        if(mIsAnimating) {
            return false;
        }
        return super.getChildStaticTransformation(child, t);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(mIsAnimating) {
            return false;
        }
        return super.onInterceptTouchEvent(ev);
    }

    public void setAnimationListener(AnimationListener animationListener) {
        mAnimationListener = animationListener;
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        if(mIsAnimating) {
            return oriDrawChild(canvas, child, drawingTime);
        }
        return super.drawChild(canvas, child, drawingTime);
    }


    public void animTo(final int toIndex) {
        if(!mIsAnimating) {
            if(null == mFrontView || null == mBackgroundView) {
                throw new IllegalStateException("null front view or background view");
            }
            if(null == mInAnimation || null == mOutAnimation) {
                throw new IllegalStateException("null animation!");
            }
            if(startAnim(toIndex)) {
                mBackgroundView.setVisibility(View.VISIBLE);
                mBackgroundView.startAnimation(mInAnimation);
                mFrontView.startAnimation(mOutAnimation);
            }
        }
    }

    private boolean startAnim(final int toIndex) {
        if(mAnimationListener != null) {
            if(mAnimationListener.onPrepare(mBackgroundView, toIndex)) {
                mIsAnimating = true;
                mInAnimRuning = true;
                mOutAnimRuning = true;
            }
        }
        return mIsAnimating;
    }

    private void stopAnim() {
        mIsAnimating = mInAnimRuning || mOutAnimRuning;
        if(!mIsAnimating) {
            swapFontAndBackground();
        }
    }

    private Animation.AnimationListener mInAnimationListener = new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mInAnimRuning = false;
            stopAnim();
        }
    };
    private Animation.AnimationListener mOutAnimationListener = new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mOutAnimRuning = false;
            stopAnim();
        }
    };

    public Animation getInAnimation() {
        return mInAnimation;
    }

    public void setInAnimation(Animation inAnimation) {
        mInAnimation = inAnimation;
        inAnimation.setAnimationListener(mInAnimationListener);
    }

    public Animation getOutAnimation() {
        return mOutAnimation;
    }

    public void setOutAnimation(Animation outAnimation) {
        mOutAnimation = outAnimation;
        outAnimation.setAnimationListener(mOutAnimationListener);
    }

    public void setInAnimation(Context context, int resourceID) {
        setInAnimation(AnimationUtils.loadAnimation(context, resourceID));
    }

    public void setOutAnimation(Context context, int resourceID) {
        setOutAnimation(AnimationUtils.loadAnimation(context, resourceID));
    }

    Animation mInAnimation;
    Animation mOutAnimation;
}
